package com.tanhua.server.controller;

import cn.hutool.core.util.StrUtil;
import com.tanhua.common.pojo.Dynamic;
import com.tanhua.common.vo.PageResult;
import com.tanhua.dubbo.server.enums.CommentType;
import com.tanhua.dubbo.server.enums.Ttype;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.QuanZiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

@RestController
@RequestMapping("movements")
public class QuanZiController {
    @Autowired
    private QuanZiService quanZiService;

    /**
     * 查询好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public PageResult findQuanZiVo(Integer page, Integer pagesize) {
        PageResult pageResult = quanZiService.findQuanZiVo(page, pagesize);
        if (pageResult == null) return null;
        return pageResult;
    }

    /**
     * 发送动态方法
     *
     * @param dynamic 数据封装实体类
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> savePublish(Dynamic dynamic) {
        //对publishId进行判断,有值代表成功
        try {
            String publishId = quanZiService.insertPublish(dynamic);
            if (StrUtil.isNotEmpty(publishId)) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询推荐动态
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public PageResult queryRecommendPublishList(Integer page, Integer pageSize) {
        return quanZiService.QueryRecommendMessage(page, pageSize);
    }

    /**
     * 点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId) {
        //指定类型为点赞
        CommentType commentType = CommentType.LIKE;
        //指定类型为添加
        Ttype tType=Ttype.INSERT;

        return getLongResponseEntity(publishId, commentType, tType);
    }
    /**
     * 取消点赞
     *
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> rmLikeComment(@PathVariable("id") String publishId) {
        //指定类型为点赞
        CommentType commentType = CommentType.LIKE;
        //指定类型为添加
        Ttype tType=Ttype.REMOVE;

        return getLongResponseEntity(publishId, commentType, tType);
    }

    /**
     * 添加喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id") String publishId) {
        //指定类型为点赞
        CommentType commentType = CommentType.LOVE;
        //指定类型为添加
        Ttype tType=Ttype.INSERT;

        return getLongResponseEntity(publishId, commentType, tType);
    }

    /**
     * 取消喜欢
     * @param publishId
     * @return
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> rmLoveComment(@PathVariable("id") String publishId) {
        //指定类型为点赞
        CommentType commentType = CommentType.LOVE;
        //指定类型为添加
        Ttype tType=Ttype.REMOVE;

        return getLongResponseEntity(publishId, commentType, tType);
    }

    /**
     * 查询单条动态
     * @param publishId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> oneComment(@PathVariable("id") String publishId) {
        QuanZiVo quanZiVo= quanZiService.oneComment(publishId);
        if (quanZiVo==null)return ResponseEntity.ok("未查询到数据!");
        return ResponseEntity.ok(quanZiVo);
    }

    /**
     * 根据类型进行点赞喜欢或取消
     * @param publishId
     * @param commentType
     * @param tType
     * @return
     */
    private ResponseEntity<Long> getLongResponseEntity( String publishId, CommentType commentType, Ttype tType) {
        try {
            //进行点赞或喜欢,并获取点赞数和喜欢数
            Long likeCount = quanZiService.arbitrarilyComment(publishId, commentType,tType);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * TODO：谁看过我
     *
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<Object> queryVisitors() {
        return ResponseEntity.ok(Collections.EMPTY_LIST);
    }
}
